#!/bin/bash
# duplicate from the web
# duplicatefromweb.sh http://heidi.pd.infn.it/html/Light_Stops/stop_stop_chi_b_Wstar_chi_b_Wstar_veto_heavy_all_final_states

################ Colors ################
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
RES_COLOR=$ESC_SEQ"39;49;00m"
RED=$ESC_SEQ"31;01m"
GREEN=$ESC_SEQ"32;01m"
YELLOW=$ESC_SEQ"33;01m"
BLUE=$ESC_SEQ"34;01m"
MAGENTA=$ESC_SEQ"35;01m"
CYAN=$ESC_SEQ"36;01m"
########################################

originaldirectory="$PWD"
baseURL="${1%/}"
foldername="${baseURL##*/}"
noninteractive=false
suffix=""

for iarg in $*; do
    CLIinputs[$argCounter]=$iarg

    option=`echo "$iarg" | cut -d"=" -f1`

    if [ "$option" == "--suffix" ]; then
	CLIsuffix=true
	suffix=`echo "$iarg" | cut -d"=" -f2`
	echo -e "the suffix will be "$RED$suffix$COL_RESET
    fi
        
    if [ "$option" == "-f" ]; then
	noninteractive=true
	echo -e $RED"this is a non-interactive run"$COL_RESET
    fi

    if [ "$option" == "-h" -o "$option" == "--help" ]; then
 
	echo -e "This script has to be launched from the folder that will contain the process folder."
	echo -e "Available options:"
	echo -e "-f"
	echo -e "--suffix=customsuffix (default is no suffix)"
	exit
    fi

    let argCounter=argCounter+1
done


echo -e "The target folder name is "$RED$foldername$COL_RESET
echo ""
thisisamgroot=false
while [ `echo $PWD | wc -c` -gt 2 -a "$thisisamgroot" = false ]; do

    if [ ! -f "./bin/mg5_aMC" ]; then
	echo -e $RED"This has to find where is the MG root folder"$COL_RESET
	echo "This is the directory: "${PWD##*/}
	echo  "... going to parent directory"
	cd ..
	pwd
    else 
	echo "it appears this is a MG5 root directory"
	thisisamgroot=true
    fi
done



rm -v proc_card_mg5.dat.fromtheweb
rm mytempproccard
wget $baseURL"/Cards/proc_card_mg5.dat" -O proc_card_mg5.dat.fromtheweb



finalpath="$originaldirectory""/""$foldername""$suffix""/"
echo -e "New process will be generated in the directory "$MAGENTA$finalpath$RES_COLOR

defaultpath=$finalpath
if [ "$noninteractive" = false ]; then
    echo -e "Please check/edit the path for the duplicate from the web"
    ####################################
    echo -e "The current destination path is "$MAGENTA$finalpath$COL_RESET
    echo -n "use it? (return=yes, or type new one) " && read finalpath && echo $finalpath
    usethispath=${finalpath:-$defaultpath}
else
usethispath="$defaultpath"
fi

echo $usethispath "will be used"

sed -i ':a;N;$!ba;s/\\\n//g' proc_card_mg5.dat.fromtheweb
cat proc_card_mg5.dat.fromtheweb | sed "s@^output\ .*@output\ $usethispath@g" > mytempproccard

diff proc_card_mg5.dat.fromtheweb mytempproccard

if [ "$noninteractive" = false ]; then
    read -p "Are you sure? " -n 1 -r
    echo    # (optional) move to a new line
fi

if [[ $REPLY =~ ^[Yy]$ || "$noninteractive" = true ]]
then
#    echo  "do dangerous stuff"

    ./bin/mg5_aMC mytempproccard
    
    cd $usethispath
    cd Cards

    wget $baseURL"/Cards/param_card.dat" -O param_card.dat
    wget $baseURL"/Cards/run_card.dat" -O run_card.dat
    wget $baseURL"/Cards/plot_card.dat" -O plot_card.dat
    wget $baseURL"/Cards/delphes_card.dat" -O delphes_card.dat
    wget $baseURL"/Cards/pgs_card.dat" -O pgs_card.dat
    wget $baseURL"/Cards/pythia_card.dat" -O pythia_card.dat

    cd ../../

    #cp mytempproccard $finalpath/Cards/proc_card_mg5.dat

fi  
